<?php
namespace Api\Controller;

use Think\Controller;


class ImgController extends SuperController
{

    //添加限制
    function _before_add(){
        $model = M('Imgcate');
        if(I('status')==1){
            //规格添加
            $gg_wdata['pid'] = I('pid');
            $gg_wdata['title'] = I('title');
            $count = $model->where($gg_wdata)->count();
            if($count>0){
                $this->ajaxErrReturn('规格已存在');
            }
        }
    }

    
    
 
     

     //删除图片
     public function dfsatw(){
        $this->LoginTrue();
        $img=$_POST['img'];
        $img2=ltrim($img,'/');
        $order_id=$_POST['order_id'];
        $ids=$_POST['ids'];
        $pattrliangti=M("pattrliangti");
        $info=$pattrliangti->where(['order_id'=>$order_id,'id'=>$ids])->find();
        if($info){
            $picc=explode(',',$info['picc']);
            if($picc){
                foreach($picc as $k=>$val){
                     if($img==$val){
                        unset($picc[$k]); 
                     }
                }
            }
            $datd=[];
            $datd['picc']=implode(',',$picc);
            if(file_exists($img2)){
                $res = unlink($img2);
            }
            $pattrliangti->where(['order_id'=>$order_id,'id'=>$ids])->save($datd);
            $data_cc=[];
            $data_cc['code']=200;
            if($res){
                $data_cc['msg']='删除成功';
            }else{
                $data_cc['msg']='图片不存在';
            }
            
        }else{
            
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='查询失败';
        }
        exit(json_encode($data_cc));
    }

     

    
    public function AddActionc()
    {
        $this->LoginTrue();
        $groups=M("imgcate");
        $data["title"]=$_POST["title"];
        $data["gNote"]=$_POST["gNote"];
        $data["status"]=$_POST["status"];
        if($_POST["status"]>=0){
            $data["pid"]=$_POST["pid"];
        }
      
        header('Content-Type:application/json; charset=utf-8');
        $result = $groups->add($data);
        if ($result) {
            $data_cc=[];
            $data_cc['code']=200;
            $data_cc['data']='';
            $data_cc['msg']='添加成功';
        } else {
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='操作失败';
        }
        exit(json_encode($data_cc));    
    }

     
    public function AddAction()
    {
        $this->LoginTrue();
        $data["title"]=$_POST["title"];
        $data["pid"]=$_POST["pid"];
        $data["status"]=$_POST["status"];
        $data["img"]=$_POST["img"];
        if($data["status"]==2){
            $data["chenfen"]=$_POST["chenfen"];
        }
        $id=$_POST["id"];
        $imgs=M("img");
        header('Content-Type:application/json; charset=utf-8');
       if($id){
            $msg="编辑";
            $result = $imgs->where(['id'=>$id])->save($data);
       }else{
            $msg="添加";
            $result = $imgs->add($data);
       }
        if ($result) {
            $data_cc=[];
            $data_cc['code']=200;
            $data_cc['data']='';
            $data_cc['msg']=$msg.'成功';
        } else {
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']=$msg.'失败';
        }
        exit(json_encode($data_cc)); 
    }

   

    //商品列表（带规格）
    function lists(){
        //生成查询条件
        $name = CONTROLLER_NAME;
        //检查过滤
        $this->check();
        $model = $model ? : D($name);
        //列表过滤器，生成查询Map对象
        if(method_exists($model,'_search')){
            $map = $model->_search();
        }else{
            $map = $this->_search();
        }
        
        if (method_exists($this, '_filter')) {
            $this->_filter($map);
        }
        if (!empty($model)) {
            if(method_exists($model,'_list')){
                $data = $model->_list($model, $map);
            }else{
                $data = $this->_list($model, $map);
            }
        }
        //echo M()->_sql();exit;
        foreach($data['list'] as &$val){
            $val['spec_list'] = $model->getMySpec($val);
            $val['color_list'] = $model->getMyColor($val);
        }
        $msg['data'] = $data;
        $this->ajaxSucReturn($msg);
    }
 
    
     
    public function updateAction()
    {
        $this->LoginTrue();
        $gId=$_GET["gId"];
        $groups=M("imgcate");
        $data["title"]=$_POST["gName"];
        $data["gNote"]=$_POST["gNote"];
        $data["status"]=$_POST["status"];
        $result = $groups->where("id=$gId")->save($data);
        if ($result) {
            $this->success("修改成功",U("catelsit"));
        } else {
            $this->error("修改失败");
        }
    }
    public function del()
    {
        $this->LoginTrue();
        $gId=$_GET["gId"];
        $img=M("img");
        $groups=M("imgcate");
        $rs=$groups->where("id=$gId")->find();
        $data_cc=[];
        header('Content-Type:application/json; charset=utf-8');
        if($rs['status']==0){
            $count_s=$img->where("pid=$gId")->count();
            if($count_s>0){
                $data_cc['code']=0;
                $data_cc['msg']='不能删除该应用，该应用下有图片';
            }
        }
        if($rs['status']==1){
            $fuliao_list=M("fuliao_list");
            $count_s3=$fuliao_list->where("cat_id=$gId")->count();
            if($count_s3>0){
                $data_cc['code']=0;
                $data_cc['msg']='不能删除该应用，该应用下有规格信息分类数据';
            }
        }
        if($rs['status']==2){
            $fuliao_mianliao=M("fuliao_mianliao");
            $count_s4=$fuliao_mianliao->where("cat_id=$gId")->count();
            if($count_s4>0){
                $data_cc['code']=0;
                $data_cc['msg']='不能删除该应用，该应用下有主辅料信息分类数据';
            }
        }
        $result = $groups->where("id=$gId")->delete();
        if ($result) {
            $data_cc=[];
            $data_cc['code']=200;
            $data_cc['data']='';
            $data_cc['msg']='删除成功';
        } else {
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='删除失败';
        }
        exit(json_encode($data_cc));    
    }
  //更新是否启用款式
  public function update_status(){
      $this->LoginTrue();
      $id=$_POST["id"];
      $status=$_POST["status"];
      $desigen_guigedetail=M("desigen_guigedetail");
      $mcc=$desigen_guigedetail->where(['design_id'=>$id])->find();
      header('Content-Type:application/json; charset=utf-8');
      if(empty($mcc)){
        $data_cc['code']=0;
        $data_cc['msg']='没有查询到数据的规格信息';
        exit(json_encode($data_cc));
      }  
      $result = $desigen_guigedetail->where(['design_id'=>$id])->save(['status'=>$status]);
      
      $data_cc=[];
      if ($result) {
          $data_cc['code']=200;
          $data_cc['msg']='操作成功';
      } else {
          $data_cc['code']=0;
          $data_cc['msg']='操作失败';
      }
      exit(json_encode($data_cc));    
  }
    public function dels()
    {
        $this->LoginTrue();
        $gId=$_POST["id"];
        $desigen_list=M("desigen_list");
        $result = $desigen_list->where(['id'=>$gId])->delete();
        header('Content-Type:application/json; charset=utf-8');
        if ($result) {
            $data_cc=[];
            $data_cc['code']=200;
            $data_cc['data']='';
            $data_cc['msg']='删除成功';
        } else {
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='删除失败';
        }
        exit(json_encode($data_cc));    
         
    }
    //删除款式列表
    public function delcc()
    {
        $this->LoginTrue();
        $data=$this->post();
        $gId=$data["id"];
        $img=M("img");
        $result = $img->where(['id'=>$gId])->delete();
        header('Content-Type:application/json; charset=utf-8');
        if ($result) {
            $data_cc=[];
            $data_cc['code']=200;
            $data_cc['data']='';
            $data_cc['msg']='删除成功';
        } else {
            $data_cc=[];
            $data_cc['code']=0;
            $data_cc['msg']='删除失败';
        }
        exit(json_encode($data_cc));    
         
    }
    //获取post数据
    function post(){
        $content = file_get_contents('php://input');
        $post    = (array)json_decode($content, true);
        return $post;
    }
}